package com.xyh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xyh.dao.BlogMapper;
import com.xyh.dao.BlogTagMapper;
import com.xyh.dao.TagMapper;
import com.xyh.dao.TypeMapper;
import com.xyh.exception.NotFoundException;
import com.xyh.pojo.Blog;
import com.xyh.pojo.PageRequest;
import com.xyh.pojo.PageResult;
import com.xyh.pojo.PageUtils;
import com.xyh.service.BlogService;
import com.xyh.util.MarkdownUtils;
import com.xyh.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;

@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private BlogTagMapper blogTagMapper;
    @Transactional
    @Override
    public int delete(Serializable id) {
        return blogMapper.delete(id);
    }

    @Transactional
    @Override
    public int save(Blog blog) {
        if (blog.getId() == null) {//保存
            blog.setCreateTime(new Date());
            blog.setUpdateTime(new Date());
            blog.setViews(0);
            String[] split = blog.getTagIds().split(",");
            typeMapper.numAdd(blog.getType().getName());
            tagMapper.numAdd(split);
            int i = blogMapper.save(blog);
            blogTagMapper.saveBlogTags(split, blog.getId());
            return i;
        }else {//更新
            blog.setUpdateTime(new Date());
            blogTagMapper.deleteBlogTag(blog);
            String[] split = blog.getTagIds().split(",");
            blogTagMapper.saveBlogTags(split, blog.getId());
            return blogMapper.update(blog.getId(),blog);
        }
    }

    @Override
    public Blog getBlog(Long id) {
        return blogMapper.findOne(id);
    }

    @Override
    public PageResult getPage(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<Blog> blogList = blogMapper.getAll();
        PageInfo<Blog> pageInfo = new PageInfo<>(blogList);
        return PageUtils.getPageResult(pageInfo);
    }

    @Override
    public PageResult getPageByCondition(PageRequest pageRequest, BlogQuery blog) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<Blog> blogList = blogMapper.getPageByCondition(blog);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogList);
        return PageUtils.getPageResult(pageInfo);
    }

    @Override
    public List<Blog> getRecommendBlogsTop(int size) {
        return blogMapper.getRecommendBlogsTop(size);
    }

    @Transactional
    @Override
    public PageResult getPageSearch(PageRequest pageRequest, String query) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<Blog> blogList = blogMapper.getPageSearch(query);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogList);
        return PageUtils.getPageResult(pageInfo);
    }

    @Override
    public Blog getAndConvert(Long id) {
        Blog blog = blogMapper.findOne(id);
        if (blog==null) {
            throw new NotFoundException("该博客不存在");
        }
        String content = blog.getContent();
        String markdown = MarkdownUtils.markdownToHtmlExtensions(content);
        blog.setContent(markdown);
        blogMapper.updateView(id);
        return blog;
    }

    @Override
    public PageResult getPageByTagId(PageRequest pageRequest, Long tagId) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<Blog> blogList = blogMapper.getBlogByTagId(tagId);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogList);
        return PageUtils.getPageResult(pageInfo);
    }

    @Override
    public Map<String, List<Blog>> archivesBlog() {
        List<String> years = blogMapper.findGroupYear();
        Map<String, List<Blog>> map=new LinkedHashMap<>();
        for (String year : years) {
            map.put(year, blogMapper.getBlogByYear(year));
        }
        return map;
    }

    @Override
    public Integer count() {
        return blogMapper.count();
    }
}
